package com.xianggu.agency.demo.impl;

import com.xianggu.agency.jdkProxy.XiangGuProxy;
import java.lang.reflect.Method;

/**
 * @author chengjy
 * @date 2025/3/18
 */
public class MyInterface2Impl implements MyInterface2 {
    private XiangGuProxy handler;
    @Override
    public void func1(java.lang.String arg0, java.lang.String arg1) {
        Method proxyMethod = null;
        try {
            proxyMethod = this.getClass().getMethod("func1", new Class[]{java.lang.String.class , java.lang.String.class }
            );
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }        int paramSize = proxyMethod.getParameterTypes().length;
        Object[] args = new Object[paramSize];
        for (int i = 0; i < paramSize; i++) {
            args[i] = "arg" + i;
        }
        if (Void.class.equals(proxyMethod.getReturnType())){
            this.handler.invoke(this , proxyMethod, args);
        }

    }

    @Override
    public String func2(java.lang.String arg0) {
        Method proxyMethod = null;
        try {
            proxyMethod = this.getClass().getMethod("func2", new Class[]{java.lang.String.class }
            );
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }        int paramSize = proxyMethod.getParameterTypes().length;
        Class<?> returnType = proxyMethod.getReturnType();
        Object[] args = new Object[paramSize];
        for (int i = 0; i < paramSize; i++) {
            args[i] = "arg" + i;
        }
        if (Void.class.equals(returnType)){
            this.handler.invoke(this , proxyMethod, args);
        }else {
            return (String) this.handler.invoke(this , proxyMethod, args);
        }
        return null;
    }

    public static void main(String[] args) {
        MyInterface2Impl interface2 = new MyInterface2Impl();
        interface2.handler = new XiangGuProxy() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) {
                System.out.println("proxy:" + proxy.getClass().getName());
                System.out.println("method:" + method.getName());
                System.out.println("args:" + args);
                return null;
            }
        };
        interface2.func2("1" );
    }
}
